$(function() {
	//删除用户
	$("#user_list").on("click", ".remove-user", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/user/deleteUser",
			dataType: 'json',
			data: {
				"id": data.id
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				if (data.code == 'success') {
					dataTablesReload.ajax.reload(null, false);
				}
				notifyMsg(data.msg);
			}
		})
	})

	//进入修改用户界面
	$("#user_list").on("click", ".edit-user", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: 'POST',
			url: "http://127.0.0.1:8080/user/getUserInfo",
			dataType: 'json',
			data: {
				"id": data.id
			},
			success: function(data) {
				var user = data.data;
				$("#userId").val(user.id);
				$("#userName").val(user.userName);
				$("#password").val(user.password);
				$("#phone").val(user.phone);
				$("#realName").val(user.realName);
				$("#email").val(user.email);
				$("#addRowModal").modal('show');
			}
		})
	})

	//新增或修改
	$("#addRowButton").click(function() {
		var id = $("#userId").val();
		var userName = $("#userName").val();
		var password = $("#password").val();
		var phone = $("#phone").val();
		var realName = $("#realName").val();
		var email = $("#email").val();
		if (userName == '') {
			notifyMsg("请输入用户名");
			return;
		}
		if (password == '') {
			notifyMsg("请输入密码");
			return;
		}
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/user/saveOrUpdate",
			dataType: "json",
			data: {
				"id": id,
				"userName": userName,
				"password": password,
				"phone": phone,
				"realName": realName,
				"email": email
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					$("#addRowModal").modal('hide');
					dataTablesReload.ajax.reload(null, false);
				}
				notifyMsg(data.msg);
			}
		});
	})

	//列表显示
	var dataTablesReload = $('#basic-datatables').DataTable({
		language: {
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sSearch": "搜索:",
			"oPaginate": {
				"sPrevious": "上页",
				"sNext": "下页"
			}
		},
		pageLength: 10,
		pagingType: "simple",
		bFilter: false,
		bLengthChange: true,
		serverSide: true,
		ordering: false,
		ajax: function(data, callback, settings) {
			var pagesize = data.length;
			var page = (data.start) / data.length + 1;
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/user/findUserListByPage",
				cache: false,
				data: {
					"pageNumber": page,
					"pageSize": pagesize
				},
				dataType: "json",
				success: function(data) {
					console.log(data)
					var code = data.code;
					if (code == 'success') {
						var returnData = {};
						returnData.recordsTotal = data.data.count;
						returnData.recordsFiltered = data.data.count;
						returnData.data = data.data.userList;
						callback(returnData);
					} else {
						notifyMsg(data.msg);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					notifyMsg('获取分页数据失败');
				}
			});
		},
		columns: [{
				"data": "userName",
				"defaultContent": "<i></i>"
			},
			{
				"data": "phone",
				"defaultContent": "<i></i>"
			},
			{
				"data": "realName",
				"defaultContent": "<i></i>"
			},
			{
				"data": "email",
				"defaultContent": "<i></i>"
			},
			{
				"data": "",
				"defaultContent": '<div class="form-button-action"><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-primary btn-lg edit-user" data-original-title="修改用户">' +
					'<i class="fa fa-edit"></i></button><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-danger remove-user" data-original-title="删除用户"><i class="fa fa-times">' +
					'</i></button></div>'
			},
		]
	});
})
